
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>动态路由 · dynamic-router</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="Gj">
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-prism/prism-tomorrow.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
        <link rel="stylesheet" href="index.css">
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    
    <link rel="prev" href="./" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    使用
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.2" data-path="dynamic.html">
            
                <a href="dynamic.html">
            
                    
                    动态路由
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >动态路由</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="dynamic-router">dynamic-router</h1>
<p><a href="https://gitee.com/gjwork/dynamic-router" target="_blank">&#x9879;&#x76EE;&#x5730;&#x5740;</a></p>
<p><a href="http://192.168.1.129:12333" target="_blank">&#x5728;&#x7EBF;&#x8BBF;&#x95EE;</a></p>
<h4 id="&#x524D;&#x8A00;">&#x524D;&#x8A00;</h4>
<blockquote>
<p><strong>&#x524D;&#x7AEF;&#xFF1A;</strong> &#x57FA;&#x4E8E; vue-element-admin &#x5EF6;&#x7528;&#x516C;&#x53F8; &#x3010;&#x7528;&#x6237;&#x4E2D;&#x5FC3;&#x3011;&#x90A3;&#x4E00;&#x5957;&#x81EA;&#x5DF1;&#x9B54;&#x6539;&#x7684;&#x7248;&#x672C;&#xFF0C;&#x5728;&#x6B64;&#x57FA;&#x7840;&#x4E4B;&#x4E0A;&#x91CD;&#x6784;&#x3002;</p>
<p><strong>&#x540E;&#x7AEF;&#xFF1A;</strong> <strong>Go</strong>&#x7684;&#x8BED;&#x6CD5;&#x7B80;&#x6D01;&#xFF0C;&#x5F00;&#x53D1;&#x901F;&#x5EA6;&#x5FEB;&#xFF0C;&#x6240;&#x4EE5;&#x540E;&#x7AEF;&#x4F7F;&#x7528;&#x4E86;<strong>Go</strong>&#x7ED3;&#x5408;<strong>Gin</strong>&#x6846;&#x67B6;&#xFF0C;&#x505A;&#x4E86;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;<strong>CURD</strong>&#x670D;&#x52A1;&#x3002;&#xFF08;&#x4E3B;&#x8981;&#x662F;&#x56E0;&#x4E3A;&#x6CA1;&#x4EBA;&#x624B;&#x534F;&#x52A9;&#xFF0C;&#x4E5F;&#x53EA;&#x597D;&#x81EA;&#x5DF1;&#x5199;&#x4E00;&#x4E2A;&#x4E86;&#x3002;&#xFF09;&#x8FD9;&#x91CC;&#x4E0D;&#x8FC7;&#x591A;&#x4ECB;&#x7ECD;&#x540E;&#x7AEF;&#x3002;</p>
<p>&#x52A8;&#x6001;&#x8DEF;&#x7531;</p>
<p>&#x7531;&#x4E8E;&#x516C;&#x53F8;&#x4E1A;&#x52A1;&#x9700;&#x8981;&#xFF0C;&#x5728;&#x6743;&#x9650;&#x9A8C;&#x8BC1;&#x4E0E;&#x5B89;&#x5168;&#x65B9;&#x9762;&#xFF0C;&#x8981;&#x6C42;&#x662F;&#x83DC;&#x5355;&#x6839;&#x636E;&#x6743;&#x9650;&#x52A8;&#x6001;&#x63A7;&#x5236;&#x3002;</p>
<p>&#x5728;&#x73B0;&#x6709;&#x7684;&#x9879;&#x76EE;&#x5F53;&#x4E2D;&#xFF0C;&#x901A;&#x8FC7;&#x57FA;&#x4E8E;  <strong>vue-element-admin</strong> &#x9879;&#x76EE;&#x63D0;&#x4F9B;&#x7684;&#x83DC;&#x5355;&#x6743;&#x9650;&#x63A7;&#x5236;&#xFF0C;&#x65E0;&#x6CD5;&#x6EE1;&#x8DB3;&#x516C;&#x53F8;&#x7684;&#x5177;&#x4F53;&#x4E1A;&#x52A1;&#x9700;&#x8981;&#x3002;</p>
<p>&#x5B9E;&#x9645;&#x4E0A;&#x4E3B;&#x8981;&#x7684;&#x76EE;&#x7684;&#x662F;&#x901A;&#x8FC7;&#x540E;&#x7AEF;&#x63A7;&#x5236;&#x83DC;&#x5355;&#x6743;&#x9650;&#x3002;</p>
<p>&#x56E0;&#x6B64;&#x4E5F;&#x8FED;&#x4EE3;&#x51FA;&#x4E86;&#x4E24;&#x4E2A;&#x7248;&#x672C;&#xFF0C;<strong>&#x7248;&#x672C;&#x4E8C;</strong>&#x4E5F;&#x53EA;&#x662F;&#x63D0;&#x4F9B;&#x4E00;&#x4E2A;&#x8BBE;&#x8BA1;&#x601D;&#x8DEF;&#xFF0C;&#x5177;&#x4F53;&#x590D;&#x6742;&#x5B9E;&#x73B0;&#x57FA;&#x4E8E;&#x6B64;&#x5957;&#x662F;&#x662F;&#x53EF;&#x4EE5;&#x6EE1;&#x8DB3;&#x7684;&#x3002;&#x8FD9;&#x91CC;&#x53EA;&#x505A;&#x7B80;&#x5355;&#x7684;&#x9610;&#x8FF0;&#xFF0C;&#x5177;&#x4F53;&#x5B9E;&#x73B0;&#x53EF;&#x4EE5;&#x7ED3;&#x5408;&#x6E90;&#x7801;&#x3002;</p>
</blockquote>
<ul>
<li><p>&#x7248;&#x672C;&#x4E00;</p>
<p>&#x5728;&#x516C;&#x53F8;&#x9879;&#x76EE;<strong>&#x3010;&#x7528;&#x6237;&#x4E2D;&#x5FC3;&#x3011;</strong>&#x4E2D;&#xFF0C;&#x6211;&#x91C7;&#x7528;&#x7684;&#x662F;&#x901A;&#x8FC7;&#x540E;&#x7AEF;&#x83DC;&#x5355;&#x5217;&#x8868;&#x4EE5;&#x53CA;&#x6743;&#x9650;&#x6807;&#x8BC6;&#xFF0C;&#x505A;&#x6DF1;&#x5EA6;&#x9012;&#x5F52;&#x548C;&#x5339;&#x914D;&#x3002;&#x8FD9;&#x4E48;&#x5199;&#x4E00;&#x90E8;&#x5206;&#x539F;&#x56E0;&#x662F;&#x56E0;&#x4E3A;&#x662F;&#x5728;&#x73B0;&#x6709;&#x7684;&#x63A5;&#x53E3;&#x6587;&#x6863;&#x57FA;&#x7840;&#x4E4B;&#x4E0A;&#x505A; <strong>&#x9B54;&#x6539;</strong>&#xFF0C;&#x7B2C;&#x4E8C;&#x70B9;&#x4E5F;&#x662F;&#x56E0;&#x4E3A;&#x4EE3;&#x7801;&#x8026;&#x5408;&#x5EA6;&#x6BD4;&#x8F83;&#x9AD8;&#xFF0C;&#x91CD;&#x6784;&#x7684;&#x8BDD;&#x5468;&#x671F;&#x957F;&#xFF08;&#x524D;&#x540E;&#x7AEF;&#x90FD;&#x9700;&#x8981;&#x6539;&#xFF0C;&#x524D;&#x7AEF;&#x5DE5;&#x4F5C;&#x91CF;&#x4F1A;&#x5F88;&#x5927;&#xFF09;&#x3002;</p>
<h4 id="interceptor">Interceptor</h4>
<pre class="language-"><code class="lang-javascript">router<span class="token punctuation">.</span><span class="token function">beforeEach</span><span class="token punctuation">(</span><span class="token keyword">async</span> <span class="token punctuation">(</span><span class="token parameter">to<span class="token punctuation">,</span> <span class="token keyword">from</span><span class="token punctuation">,</span> next</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  NProgress<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">getToken</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>to<span class="token punctuation">.</span>path <span class="token operator">===</span> RouterPath<span class="token punctuation">.</span><span class="token constant">LOGIN</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">{</span> path<span class="token operator">:</span> RouterPath<span class="token punctuation">.</span><span class="token constant">HOME</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      NProgress<span class="token punctuation">.</span><span class="token function">done</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
      <span class="token keyword">try</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>store<span class="token punctuation">.</span>getters<span class="token punctuation">.</span>routerLoadDone<span class="token punctuation">)</span> <span class="token punctuation">{</span>
          <span class="token keyword">const</span> <span class="token punctuation">{</span> isAdmin <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token keyword">await</span> store<span class="token punctuation">.</span><span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token string">&quot;getUserInfo&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
          <span class="token keyword">const</span> accessRoutes <span class="token operator">=</span> <span class="token keyword">await</span> store<span class="token punctuation">.</span><span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token string">&quot;generateRoutes&quot;</span><span class="token punctuation">,</span> isAdmin<span class="token punctuation">)</span><span class="token punctuation">;</span>
          router<span class="token punctuation">.</span><span class="token function">addRoutes</span><span class="token punctuation">(</span>accessRoutes<span class="token punctuation">)</span><span class="token punctuation">;</span>
          <span class="token keyword">let</span> query <span class="token operator">=</span> to<span class="token punctuation">.</span>query<span class="token punctuation">;</span>
          query<span class="token punctuation">.</span>token <span class="token operator">=</span> <span class="token function">getToken</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
          <span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token operator">...</span>to<span class="token punctuation">,</span> replace<span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span> query <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
          <span class="token operator">...</span>
          <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
      <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span>error<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token operator">...</span>
        NProgress<span class="token punctuation">.</span><span class="token function">done</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>whiteList<span class="token punctuation">.</span><span class="token function">includes</span><span class="token punctuation">(</span>to<span class="token punctuation">.</span>path<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
      <span class="token function">next</span><span class="token punctuation">(</span>RouterPath<span class="token punctuation">.</span><span class="token constant">LOGIN</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      NProgress<span class="token punctuation">.</span><span class="token function">done</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<h4 id="vuex">Vuex</h4>
<pre class="language-"><code class="lang-javascript">actions<span class="token operator">:</span> <span class="token punctuation">{</span>
    <span class="token operator">...</span>
    <span class="token keyword">async</span> <span class="token function">generateRoutes</span><span class="token punctuation">(</span><span class="token parameter"><span class="token punctuation">{</span> commit <span class="token punctuation">}</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">const</span> <span class="token punctuation">{</span> body<span class="token punctuation">,</span> status <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token keyword">await</span> <span class="token function">getUserMenuAndPermission</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
        appId<span class="token operator">:</span> <span class="token operator">+</span>AppId<span class="token punctuation">,</span>
      <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token keyword">if</span> <span class="token punctuation">(</span>status <span class="token operator">===</span> <span class="token number">200</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">const</span> <span class="token punctuation">{</span> menus<span class="token punctuation">,</span> permissions <span class="token punctuation">}</span> <span class="token operator">=</span> body<span class="token punctuation">;</span>
        <span class="token function">commit</span><span class="token punctuation">(</span><span class="token string">&quot;SET_ROLES&quot;</span><span class="token punctuation">,</span> permissions<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">const</span> accessedRoutes <span class="token operator">=</span> <span class="token function">LoadMenus</span><span class="token punctuation">(</span>menus<span class="token punctuation">,</span> asyncRoutes<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// isAdmin</span>
        <span class="token comment">//   ? matchMenuResource(menus, asyncRoutes)</span>
        <span class="token comment">//   : LoadMenus(menus, asyncRoutes);</span>
        <span class="token function">commit</span><span class="token punctuation">(</span><span class="token string">&quot;SET_ROUTES&quot;</span><span class="token punctuation">,</span> accessedRoutes<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> accessedRoutes<span class="token punctuation">;</span>
      <span class="token punctuation">}</span>
      <span class="token function">commit</span><span class="token punctuation">(</span><span class="token string">&quot;SET_ROUTES&quot;</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token keyword">return</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
</code></pre>
<h4 id="utilsmenus">Utils/Menus</h4>
<pre class="language-"><code class="lang-javascript"><span class="token keyword">import</span> <span class="token punctuation">{</span> MenuType<span class="token punctuation">,</span> MenuStatus <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">&quot;@/utils/Enum/Menu&quot;</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> _ <span class="token keyword">from</span> <span class="token string">&quot;lodash&quot;</span><span class="token punctuation">;</span>

<span class="token comment">/* &#x767D;&#x540D;&#x5355; */</span>
<span class="token keyword">const</span> whiteList <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">&quot;404&quot;</span><span class="token punctuation">]</span><span class="token punctuation">;</span>

<span class="token comment">/**
 * &#x6839;&#x636E;&#x6743;&#x9650; &#x52A0;&#x8F7D;&#x83DC;&#x5355;
 * @param {Array} assessedMenus &#x6743;&#x9650;&#x83DC;&#x5355;
 * @param {Array} asyncRoutes &#x9884;&#x52A0;&#x8F7D;&#x83DC;&#x5355;
 */</span>
<span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">LoadMenus</span><span class="token punctuation">(</span><span class="token parameter">assessedMenus<span class="token punctuation">,</span> asyncRoutes</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> <span class="token punctuation">{</span> <span class="token constant">CATALOG</span> <span class="token punctuation">}</span> <span class="token operator">=</span> MenuType<span class="token punctuation">;</span>
  <span class="token keyword">const</span> <span class="token punctuation">{</span> <span class="token constant">ENABLE</span> <span class="token punctuation">}</span> <span class="token operator">=</span> MenuStatus<span class="token punctuation">;</span>

  <span class="token comment">/** &#x5148;&#x9884;&#x52A0;&#x8F7D; &#x9700;&#x8981;&#x6DFB;&#x52A0;&#x7684;&#x83DC;&#x5355; */</span>
  <span class="token keyword">let</span> menuCache <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
    childCache <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
  asyncRoutes<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">item</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token keyword">const</span> <span class="token punctuation">{</span> meta<span class="token punctuation">,</span> children <span class="token punctuation">}</span> <span class="token operator">=</span> item<span class="token punctuation">;</span>
    <span class="token comment">/* &#x76EE;&#x5F55; */</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>meta<span class="token punctuation">)</span> menuCache<span class="token punctuation">[</span>meta<span class="token punctuation">.</span>title<span class="token punctuation">]</span> <span class="token operator">=</span> item<span class="token punctuation">;</span>
    <span class="token comment">/* &#x83DC;&#x5355; */</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>children<span class="token punctuation">)</span> <span class="token function">childsForMap</span><span class="token punctuation">(</span>childCache<span class="token punctuation">,</span> children<span class="token punctuation">,</span> meta<span class="token punctuation">.</span>title<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token comment">/* &#x9700;&#x8981;&#x52A0;&#x8F7D;&#x7684;&#x76EE;&#x5F55; */</span>
  <span class="token keyword">let</span> resultArray <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>

  assessedMenus
    <span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">a<span class="token punctuation">,</span> b</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> a<span class="token punctuation">.</span>orderNo <span class="token operator">-</span> b<span class="token punctuation">.</span>orderNo<span class="token punctuation">)</span>
    <span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">item</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
      <span class="token keyword">const</span> <span class="token punctuation">{</span> menuType<span class="token punctuation">,</span> menuPerms<span class="token punctuation">,</span> menuIcon<span class="token punctuation">,</span> subMenus<span class="token punctuation">,</span> status <span class="token punctuation">}</span> <span class="token operator">=</span> item<span class="token punctuation">;</span>
      <span class="token comment">/* &#x76EE;&#x5F55; */</span>
      <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">+</span>menuType <span class="token operator">===</span> <span class="token constant">CATALOG</span> <span class="token operator">&amp;&amp;</span> <span class="token operator">+</span>status <span class="token operator">===</span> <span class="token constant">ENABLE</span> <span class="token operator">&amp;&amp;</span> menuCache<span class="token punctuation">[</span>menuPerms<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        menuCache<span class="token punctuation">[</span>menuPerms<span class="token punctuation">]</span><span class="token punctuation">.</span>children <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
        menuCache<span class="token punctuation">[</span>menuPerms<span class="token punctuation">]</span><span class="token punctuation">.</span>children <span class="token operator">=</span> <span class="token function">getChildMenus</span><span class="token punctuation">(</span>
          subMenus<span class="token punctuation">,</span>
          childCache<span class="token punctuation">,</span>
          menuPerms
        <span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">/* &#x6DFB;&#x52A0; menuIcon */</span>
        menuCache<span class="token punctuation">[</span>menuPerms<span class="token punctuation">]</span><span class="token punctuation">.</span>menuIcon <span class="token operator">=</span> menuIcon<span class="token punctuation">;</span>
        resultArray<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>menuCache<span class="token punctuation">[</span>menuPerms<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token comment">/* &#x6DFB;&#x52A0;&#x767D;&#x540D;&#x5355; */</span>
  whiteList<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">white</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>menuCache<span class="token punctuation">[</span>white<span class="token punctuation">]</span><span class="token punctuation">)</span> resultArray<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>menuCache<span class="token punctuation">[</span>white<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token keyword">return</span> resultArray<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">/**
 * &#x83B7;&#x53D6;&#x5B50;&#x83DC;&#x5355;&#x5217;&#x8868;
 * @param {Array} subMenus &#x6743;&#x9650;&#x5B50;&#x83DC;&#x5355;
 * @param {Object} asyncChildMap &#x9884;&#x52A0;&#x8F7D;&#x5B50;&#x83DC;&#x5355; Map
 * @param {String} pCode &#x7236;&#x7EA7;&#x6743;&#x9650;&#x6807;&#x8BC6;
 */</span>
<span class="token keyword">function</span> <span class="token function">getChildMenus</span><span class="token punctuation">(</span><span class="token parameter">subMenus<span class="token punctuation">,</span> childCache<span class="token punctuation">,</span> pCode</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> <span class="token punctuation">{</span> <span class="token constant">MENU</span> <span class="token punctuation">}</span> <span class="token operator">=</span> MenuType<span class="token punctuation">;</span>
  <span class="token keyword">const</span> <span class="token punctuation">{</span> <span class="token constant">ENABLE</span> <span class="token punctuation">}</span> <span class="token operator">=</span> MenuStatus<span class="token punctuation">;</span>

  <span class="token comment">/* &#x5B50;&#x83DC;&#x5355;&#x96C6;&#x5408; */</span>
  <span class="token keyword">let</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  subMenus
    <span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">a<span class="token punctuation">,</span> b</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> a<span class="token punctuation">.</span>orderNo <span class="token operator">-</span> b<span class="token punctuation">.</span>orderNo<span class="token punctuation">)</span>
    <span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">item</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
      <span class="token keyword">const</span> <span class="token punctuation">{</span> menuPerms<span class="token punctuation">,</span> menuType<span class="token punctuation">,</span> menuIcon<span class="token punctuation">,</span> status <span class="token punctuation">}</span> <span class="token operator">=</span> item<span class="token punctuation">;</span>
      <span class="token keyword">const</span> _childMenu <span class="token operator">=</span> childCache<span class="token punctuation">[</span>menuPerms<span class="token punctuation">]</span><span class="token punctuation">;</span>
      <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">+</span>menuType <span class="token operator">===</span> <span class="token constant">MENU</span> <span class="token operator">&amp;&amp;</span> <span class="token operator">+</span>status <span class="token operator">===</span> <span class="token constant">ENABLE</span> <span class="token operator">&amp;&amp;</span> _childMenu<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">/* &#x6DFB;&#x52A0; menuIcon */</span>
        childCache<span class="token punctuation">[</span>menuPerms<span class="token punctuation">]</span><span class="token punctuation">.</span>menuIcon <span class="token operator">=</span> menuIcon<span class="token punctuation">;</span>
        arr<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>childCache<span class="token punctuation">[</span>menuPerms<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token comment">/* &#x6DFB;&#x52A0; hidden&#x9875;&#x9762; */</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>childCache<span class="token punctuation">[</span>pCode<span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> childCache<span class="token punctuation">[</span>pCode<span class="token punctuation">]</span><span class="token punctuation">.</span>length <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">)</span>
    arr<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token operator">...</span>childCache<span class="token punctuation">[</span>pCode<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">return</span> arr<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">/**
 * &#x5B50;&#x96C6;&#x83DC;&#x5355; &#x505A;Map
 * @param {Object} cache &#x5B50;&#x96C6;&#x83DC;&#x5355;hMap
 * @param {Array} childs &#x9884;&#x5904;&#x7406;&#x5B50;&#x96C6;&#x5217;&#x8868;
 * @param {String} pCode &#x7236;&#x7EA7;&#x6743;&#x9650;&#x6807;&#x8BC6;
 */</span>
<span class="token keyword">function</span> <span class="token function">childsForMap</span><span class="token punctuation">(</span><span class="token parameter">cache<span class="token punctuation">,</span> childs<span class="token punctuation">,</span> pCode</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  cache<span class="token punctuation">[</span>pCode<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  childs<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">item</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token keyword">const</span> <span class="token punctuation">{</span> hidden<span class="token punctuation">,</span> meta <span class="token punctuation">}</span> <span class="token operator">=</span> item<span class="token punctuation">;</span>
    <span class="token comment">/* &#x6DFB;&#x52A0;&#x9690;&#x85CF;&#x9875; */</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>hidden<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      cache<span class="token punctuation">[</span>pCode<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>item<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> cache<span class="token punctuation">[</span>meta<span class="token punctuation">.</span>title<span class="token punctuation">]</span> <span class="token operator">=</span> item<span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">/* &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; */</span>

<span class="token comment">/**
 * &#x4E3A;&#x89E3;&#x51B3;&#x83DC;&#x5355;&#x6E32;&#x67D3;&#x65F6;&#xFF0C;&#x66FF;&#x6362;&#x6743;&#x9650;&#x83DC;&#x5355;&#x6709;&#x6548;&#x4FE1;&#x606F;&#xFF1A;menuIcon
 * @param {Array} assessedMenus &#x6743;&#x9650;&#x83DC;&#x5355;
 * @param {Array} asyncRoutes &#x9884;&#x52A0;&#x8F7D;&#x83DC;&#x5355;
 */</span>
<span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">matchMenuResource</span><span class="token punctuation">(</span><span class="token parameter">assessedMenus<span class="token punctuation">,</span> asyncRoutes</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment">/** &#x8D44;&#x6E90;&#x83DC;&#x5355; to Map */</span>
  <span class="token keyword">let</span> menuCache <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>

  <span class="token keyword">const</span> <span class="token punctuation">{</span> <span class="token constant">ENABLE</span> <span class="token punctuation">}</span> <span class="token operator">=</span> MenuStatus<span class="token punctuation">;</span>
  assessedMenus<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter"><span class="token punctuation">{</span> menuPerms<span class="token punctuation">,</span> menuIcon<span class="token punctuation">,</span> subMenus<span class="token punctuation">,</span> status <span class="token punctuation">}</span></span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token comment">/* &#x542F;&#x7528;&#x72B6;&#x6001; */</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">+</span>status <span class="token operator">===</span> <span class="token constant">ENABLE</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token comment">/* &#x76EE;&#x5F55; cache */</span>
      <span class="token keyword">if</span> <span class="token punctuation">(</span>menuIcon<span class="token punctuation">)</span> menuCache<span class="token punctuation">[</span>menuPerms<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">{</span> menuIcon <span class="token punctuation">}</span><span class="token punctuation">;</span>
      <span class="token comment">/* &#x83DC;&#x5355; cache */</span>
      <span class="token keyword">if</span> <span class="token punctuation">(</span>_<span class="token punctuation">.</span><span class="token function">isArray</span><span class="token punctuation">(</span>subMenus<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token function">cacheSubMenus</span><span class="token punctuation">(</span>subMenus<span class="token punctuation">,</span> menuCache<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token comment">/** Router&#x83DC;&#x5355; */</span>
  asyncRoutes<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">item</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token keyword">const</span> <span class="token punctuation">{</span> meta<span class="token punctuation">,</span> children <span class="token punctuation">}</span> <span class="token operator">=</span> item<span class="token punctuation">;</span>
    <span class="token comment">/* &#x76EE;&#x5F55; */</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>meta <span class="token operator">&amp;&amp;</span> menuCache<span class="token punctuation">[</span>meta<span class="token punctuation">.</span>title<span class="token punctuation">]</span><span class="token punctuation">)</span>
      item<span class="token punctuation">.</span>menuIcon <span class="token operator">=</span> menuCache<span class="token punctuation">[</span>meta<span class="token punctuation">.</span>title<span class="token punctuation">]</span><span class="token punctuation">.</span>menuIcon<span class="token punctuation">;</span>
    <span class="token comment">/* &#x83DC;&#x5355; */</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>children<span class="token punctuation">)</span> <span class="token function">matchChildMenus</span><span class="token punctuation">(</span>children<span class="token punctuation">,</span> menuCache<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">return</span> asyncRoutes<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">/**
 * &#x7F13;&#x5B58;&#x83DC;&#x5355;
 * @param {Array} subMenus &#x83DC;&#x5355;&#x5217;&#x8868;
 * @param {Object} menuCache &#x8D44;&#x6E90;&#x83DC;&#x5355;Map
 */</span>
<span class="token keyword">function</span> <span class="token function">cacheSubMenus</span><span class="token punctuation">(</span><span class="token parameter">subMenus<span class="token punctuation">,</span> menuCache</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> <span class="token punctuation">{</span> <span class="token constant">ENABLE</span> <span class="token punctuation">}</span> <span class="token operator">=</span> MenuStatus<span class="token punctuation">;</span>
  subMenus<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter"><span class="token punctuation">{</span> menuPerms<span class="token punctuation">,</span> menuIcon<span class="token punctuation">,</span> status <span class="token punctuation">}</span></span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">+</span>status <span class="token operator">===</span> <span class="token constant">ENABLE</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">if</span> <span class="token punctuation">(</span>menuIcon<span class="token punctuation">)</span> menuCache<span class="token punctuation">[</span>menuPerms<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">{</span> menuIcon <span class="token punctuation">}</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">/**
 * &#x5339;&#x914D;&#x83DC;&#x5355;
 * @param {Array} children &#x83DC;&#x5355;&#x5217;&#x8868;
 * @param {Object} menuCache &#x8D44;&#x6E90;&#x83DC;&#x5355;Map
 */</span>
<span class="token keyword">function</span> <span class="token function">matchChildMenus</span><span class="token punctuation">(</span><span class="token parameter">children<span class="token punctuation">,</span> menuCache</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  children<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">item</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token keyword">const</span> <span class="token punctuation">{</span> meta <span class="token punctuation">}</span> <span class="token operator">=</span> item<span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>meta <span class="token operator">&amp;&amp;</span> menuCache<span class="token punctuation">[</span>meta<span class="token punctuation">.</span>title<span class="token punctuation">]</span><span class="token punctuation">)</span>
      item<span class="token punctuation">.</span>menuIcon <span class="token operator">=</span> menuCache<span class="token punctuation">[</span>meta<span class="token punctuation">.</span>title<span class="token punctuation">]</span><span class="token punctuation">.</span>menuIcon<span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
</li>
</ul>
<ul>
<li><p>&#x7248;&#x672C;&#x4E8C;</p>
<p>&#x7F51;&#x4E0A;&#x7684;&#x535A;&#x5BA2;&#x5206;&#x4EAB;&#x7684;&#x7B2C;&#x4E8C;&#x79CD;&#x89E3;&#x51B3;&#x65B9;&#x6848;</p>
<p>&#x9644;&#x4E0A;&#x94FE;&#x63A5; <a href="https://segmentfault.com/a/1190000021900731" target="_blank">https://segmentfault.com/a/1190000021900731</a></p>
<p>&#x94FE;&#x63A5;2 <a href="https://www.jianshu.com/p/ceef589de5e9" target="_blank">https://www.jianshu.com/p/ceef589de5e9</a></p>
<p>&#x8FD9;&#x4E24;&#x7BC7;&#x535A;&#x5BA2;&#x7684;&#x6587;&#x6863;&#x5927;&#x81F4;&#x770B;&#x4E86;&#x4E00;&#x4E0B;&#xFF0C;&#x601D;&#x8DEF;&#x662F;&#x4E00;&#x6837;&#x7684;&#xFF0C;&#x539F;&#x7406;&#x5B9E;&#x9645;&#x4E0A;&#x5C31;&#x662F;&#x628A; <strong>Vue-Router</strong> &#x91CC;&#x9762;&#x7684;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x5168;&#x90E8;&#x653E;&#x5230;&#x540E;&#x7AEF;&#x505A; <strong>Json</strong> &#x5B58;&#x50A8;&#x3002;&#x7279;&#x6B8A;&#x5B57;&#x6BB5;&#x5982;&#xFF1A;<strong>components</strong>&#xFF0C;&#x5728;&#x53D6;&#x51FA;&#x7684;&#x540C;&#x65F6;&#x52A8;&#x6001; <strong>require</strong>&#x3002;</p>
<p>&#x4F46;&#x5982;&#x679C;&#x7ED3;&#x5408;&#x516C;&#x53F8;&#x4E1A;&#x52A1;&#xFF0C;&#x5728;<strong>&#x3010;&#x7528;&#x6237;&#x4E2D;&#x5FC3;&#x3011;</strong>&#x9879;&#x76EE;&#x4E0B;&#x7684;<strong>&#x83DC;&#x5355;&#x7BA1;&#x7406;</strong>&#x9875;&#x9762;&#xFF0C;&#x624B;&#x52A8;&#x6DFB;&#x52A0; <strong>&#x76EE;&#x5F55;&#x3001;&#x83DC;&#x5355;&#x3001;icon&#x3001;path</strong> &#x8FD9;&#x79CD;&#x65B9;&#x5F0F;&#x7528;&#x8D77;&#x6765;&#x611F;&#x89C9;&#x8FD8;&#x662F;&#x4E0D;&#x592A;&#x53CB;&#x597D;&#x3002;</p>
<p><img src="https://img2020.cnblogs.com/blog/1505182/202007/1505182-20200730233703533-468627170.png" alt=""></p>
<p>&#x7248;&#x672C;&#x4E8C;&#x6211;&#x8BBE;&#x8BA1;&#x7684;&#x601D;&#x8DEF;&#x662F;&#x52A8;&#x9759;&#x5206;&#x79BB;&#xFF0C;Router&#x6587;&#x4EF6;&#x7684;&#x56FA;&#x5B9A;&#x683C;&#x5F0F;&#x4E0D;&#x53D8;&#xFF0C;&#x5177;&#x4F53;&#x7684;&#x540D;&#x79F0;&#x3001;&#x8DEF;&#x5F84;&#x505A;&#x62C6;&#x5206;&#xFF0C;&#x5206;&#x522B;&#x5B58;&#x5230;&#x524D;&#x7AEF;&#x548C;&#x540E;&#x7AEF;&#x3002;&#x5177;&#x4F53;&#x5B9E;&#x73B0;&#x65B9;&#x5F0F;&#x5982;&#x4E0B;&#x6587;&#x3002;</p>
</li>
</ul>
<h4 id="&#x52A8;&#x6001;&#x8DEF;&#x7531;">&#x52A8;&#x6001;&#x8DEF;&#x7531;</h4>
<blockquote>
<p>&#x9644;&#x4E0A;&#x6E90;&#x7801;</p>
<p>&#x6211;&#x7684;&#x601D;&#x8DEF;&#x662F;&#x9700;&#x8981;&#x6743;&#x9650;&#x63A7;&#x5236;&#x7684;&#x83DC;&#x5355;&#x5206;&#x6210;&#x4E09;&#x90E8;&#x5206;&#xFF1A;</p>
</blockquote>
<ol>
<li><p><strong>MenuMap</strong> &#x505A;&#x89C6;&#x56FE;&#x7EC4;&#x4EF6;</p>
<pre class="language-"><code class="lang-javascript"><span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span>
  <span class="token string">&quot;/user/manage&quot;</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token keyword">import</span><span class="token punctuation">(</span><span class="token string">&quot;@/views/user/index&quot;</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
  <span class="token string">&quot;/system/manage&quot;</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token keyword">import</span><span class="token punctuation">(</span><span class="token string">&quot;@/views/system/index&quot;</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre>
</li>
</ol>
<ol>
<li><p><strong>RouterJson</strong> &#x505A;&#x5DE5;&#x5382;&#x51FD;&#x6570;</p>
<pre class="language-"><code class="lang-javascript"><span class="token comment">/**
 * &#x751F;&#x6210;&#x8DEF;&#x7531;json
 */</span>
<span class="token keyword">function</span> <span class="token function">getRouterJson</span><span class="token punctuation">(</span><span class="token parameter">headPath<span class="token punctuation">,</span> childPath<span class="token punctuation">,</span> menuName<span class="token punctuation">,</span> component</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token punctuation">{</span>
    path<span class="token operator">:</span> headPath<span class="token punctuation">,</span>
    name<span class="token operator">:</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">,</span>
    component<span class="token operator">:</span> Layout<span class="token punctuation">,</span>
    meta<span class="token operator">:</span> <span class="token punctuation">{</span>
      title<span class="token operator">:</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">,</span>
      <span class="token comment">//   icon: &quot;&quot;,</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    children<span class="token operator">:</span> <span class="token punctuation">[</span>
      <span class="token punctuation">{</span>
        path<span class="token operator">:</span> childPath<span class="token punctuation">,</span>
        name<span class="token operator">:</span> headPath <span class="token operator">+</span> childPath<span class="token punctuation">,</span>
        component<span class="token operator">:</span> component<span class="token punctuation">,</span>
        meta<span class="token operator">:</span> <span class="token punctuation">{</span>
          title<span class="token operator">:</span> menuName<span class="token punctuation">,</span>
          <span class="token comment">//   icon: &quot;&quot;,</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span>
      <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">]</span><span class="token punctuation">,</span>
  <span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">/**
 * 404&#x9875;&#x9762;
 */</span>
<span class="token keyword">function</span> <span class="token function">notFound</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token punctuation">{</span> path<span class="token operator">:</span> <span class="token string">&quot;*&quot;</span><span class="token punctuation">,</span> redirect<span class="token operator">:</span> <span class="token string">&quot;/404&quot;</span><span class="token punctuation">,</span> meta<span class="token operator">:</span> <span class="token punctuation">{</span> title<span class="token operator">:</span> <span class="token string">&quot;404&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> hidden<span class="token operator">:</span> <span class="token boolean">true</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
</li>
</ol>
<ol>
<li><p>&#x52A8;&#x9759;&#x5206;&#x79BB;</p>
<blockquote>
<p>&#x901A;&#x8FC7; <strong>Interceptor</strong> &#x8C03;&#x7528;&#x540E;&#x7AEF;&#x63A5;&#x53E3;&#x83B7;&#x53D6; <code>path&#x3001;name&#x3001;icon&#x7B49;</code>&#x6743;&#x9650;&#x83DC;&#x5355;&#x5217;&#x8868;</p>
<p>&#x7ED3;&#x5408; Vuex&#x4E0B; <strong>generateRoutes</strong> &#x51FD;&#x6570;&#x7EC4;&#x88C5;&#x8DEF;&#x7531;&#x3002;</p>
</blockquote>
<pre class="language-"><code class="lang-javascript"><span class="token keyword">import</span> Layout <span class="token keyword">from</span> <span class="token string">&quot;@/layout/index.vue&quot;</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> menuData <span class="token keyword">from</span> <span class="token string">&quot;@/router/menu&quot;</span><span class="token punctuation">;</span>

<span class="token operator">...</span>
<span class="token operator">...</span>

<span class="token comment">/**
 * &#x62C6;&#x5206;path
 * @param {string} path
 */</span>
<span class="token keyword">function</span> <span class="token function">splitPath</span><span class="token punctuation">(</span><span class="token parameter">path</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> list <span class="token operator">=</span> path<span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token string">&quot;/&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">let</span> headPath <span class="token operator">=</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">/</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>list<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">;</span>
  <span class="token keyword">let</span> childPath <span class="token operator">=</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">;</span>
  list<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">item<span class="token punctuation">,</span> index</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>index <span class="token operator">&gt;</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      childPath <span class="token operator">+=</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">/</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>item<span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">return</span> <span class="token punctuation">{</span> headPath<span class="token punctuation">,</span> childPath<span class="token operator">:</span> childPath<span class="token punctuation">.</span><span class="token function">substr</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> childPath<span class="token punctuation">.</span>length <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">/**
 * &#x6839;&#x636E;&#x6743;&#x9650; &#x52A0;&#x8F7D;&#x83DC;&#x5355;
 * @param {Array} assessedMenus &#x6743;&#x9650;&#x83DC;&#x5355;
 * @param {Array} asyncRoutes &#x9884;&#x52A0;&#x8F7D;&#x83DC;&#x5355;
 */</span>
<span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">LoadMenus</span><span class="token punctuation">(</span><span class="token parameter">assessedMenus</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">let</span> resultArray <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  assessedMenus<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">item</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token keyword">const</span> <span class="token punctuation">{</span> name<span class="token punctuation">,</span> path <span class="token punctuation">}</span> <span class="token operator">=</span> item<span class="token punctuation">;</span>
    <span class="token keyword">const</span> <span class="token punctuation">{</span> headPath<span class="token punctuation">,</span> childPath <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token function">splitPath</span><span class="token punctuation">(</span>item<span class="token punctuation">.</span>path<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>menuData<span class="token punctuation">[</span>path<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      resultArray<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>
        <span class="token function">getRouterJson</span><span class="token punctuation">(</span>headPath<span class="token punctuation">,</span> childPath<span class="token punctuation">,</span> name<span class="token punctuation">,</span> menuData<span class="token punctuation">[</span>path<span class="token punctuation">]</span><span class="token punctuation">)</span>
      <span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
      resultArray<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>
        <span class="token function">getRouterJson</span><span class="token punctuation">(</span>headPath<span class="token punctuation">,</span> childPath<span class="token punctuation">,</span> item<span class="token punctuation">.</span>name<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span>
          <span class="token keyword">import</span><span class="token punctuation">(</span><span class="token string">&quot;@/views/404/index&quot;</span><span class="token punctuation">)</span>
        <span class="token punctuation">)</span>
      <span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  resultArray<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token function">notFound</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">return</span> resultArray<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
</li>
</ol>
<h4 id="&#x6548;&#x679C;&#x5C55;&#x793A;">&#x6548;&#x679C;&#x5C55;&#x793A;</h4>
<p><img src="dynamic-router.gif" alt=""></p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="./" class="navigation navigation-prev navigation-unique" aria-label="Previous page: 使用">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"动态路由","level":"1.2","depth":1,"previous":{"title":"使用","level":"1.1","depth":1,"path":"README.md","ref":"README.md","articles":[]},"dir":"ltr"},"config":{"plugins":["expandable-chapters","-lunr","-search","search-pro","-highlight","prism"],"styles":{"website":"index.css"},"pluginsConfig":{"prism":{"css":["prismjs/themes/prism-tomorrow.css"]},"expandable-chapters":{},"search-pro":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"Gj","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"dynamic-router","language":"zh-hans","gitbook":"3.2.3","description":"动态路由"},"file":{"path":"dynamic.md","mtime":"2020-07-31T07:40:36.799Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-07-31T07:40:46.045Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

